﻿Namespace EIBData.DPT
    Public NotInheritable Class EIBDataDPT_21_001
        Inherits EIBDataDPT_21_Base

        Protected _OutOfService As Boolean = Nothing
        Protected _Fault As Boolean = Nothing
        Protected _Overridden As Boolean = Nothing
        Protected _InAlarm As Boolean = Nothing
        Protected _AlarmUnAck As Boolean = Nothing


        Sub New(data As Byte())
            MyBase.New(data)

            _OutOfService = _valueBase And &H1
            _Fault = (_valueBase And &H2) >> 1
            _Overridden = (_valueBase And &H4) >> 2
            _InAlarm = (_valueBase And &H8) >> 3
            _AlarmUnAck = (_valueBase And &H10) >> 4
        End Sub

        Sub New(OutOfService As Boolean, Fault As Boolean, Overridden As Boolean, InAlarm As Boolean, AlarmUnAck As Boolean)
            MyBase.New((If(AlarmUnAck, 1, 0) << 4) + (If(InAlarm, 1, 0) << 3) + (If(Overridden, 1, 0) << 2) + (If(Fault, 1, 0) << 1) + If(OutOfService, 1, 0))

            _OutOfService = OutOfService
            _Fault = Fault
            _Overridden = Overridden
            _InAlarm = InAlarm
            _AlarmUnAck = AlarmUnAck
        End Sub


        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", DPT_Name))
            list.Add(New EIB.Info("OutOfService", String.Format("{0} (0x{0:X})", _OutOfService)))
            list.Add(New EIB.Info("Fault", String.Format("{0} (0x{0:X})", _Fault)))
            list.Add(New EIB.Info("Overridden", String.Format("{0} (0x{0:X})", _Overridden)))
            list.Add(New EIB.Info("InAlarm", String.Format("{0} (0x{0:X})", _InAlarm)))
            list.Add(New EIB.Info("AlarmUnAck", String.Format("{0} (0x{0:X})", _AlarmUnAck)))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property DPT_Name As String
            Get
                Return "DPT_StatusGen"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Number As String
            Get
                Return "21.001"
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return String.Format("OutOfService: {0}, Fault: {1}, Overridden: {2}, InAlarm: {3}, AlarmUnAck: {4}", _OutOfService, _Fault, _Overridden, _InAlarm, _AlarmUnAck)
        End Function

        Public Overloads ReadOnly Property OutOfService As Boolean
            Get
                Return _OutOfService
            End Get
        End Property

        Public Overloads ReadOnly Property Fault As Boolean
            Get
                Return _Fault
            End Get
        End Property

        Public Overloads ReadOnly Property Overridden As Boolean
            Get
                Return _Overridden
            End Get
        End Property

        Public Overloads ReadOnly Property InAlarm As Boolean
            Get
                Return _InAlarm
            End Get
        End Property

        Public Overloads ReadOnly Property AlarmUnAck As Boolean
            Get
                Return _AlarmUnAck
            End Get
        End Property
    End Class
End Namespace